#include "zdfl_parameter_define.h"
#include "app_OSD.h"
#include "zdfl_core_mwui.h"
#include "zdfl_app_ctrl.h"
#include "User_handle.h"


#define MAX_FONT_SET    6
#define InvalidPageNumber(A) (A->hParent >= sizeof(app_ui_pages)/sizeof(void*))

//////////////////////////////////////////////////


static void zprintCtrlCoreValue(int id, int x, int y);
void ZApp_DrawIcon_Scratch_page( U8 user_scratch_page_id, U16 Icon_id, UIControlIcon_struct *pIcon);
void ZApp_DrawText_Scratch_page( U8 user_scratch_page_id, U16 Text_id, UIControlString_struct *pText);

void DrawImage(U16 image_index);
void DrawFont(int textID);
void DrawImage_WH(int imageID, int x, int y, int w, int h);

#ifdef Keil_C51
int BtnCallBack (  msg_data_t * MSG )reentrant;
int BtnCallBack1 (  msg_data_t * MSG )reentrant;
int PageCallBack (  msg_data_t * MSG )reentrant;
int TextCallBack (  msg_data_t * MSG )reentrant;
int ImageCallBack (  msg_data_t * MSG )reentrant;
int ProgCallBack (  msg_data_t * MSG )reentrant;
int ScrCallBack (  msg_data_t * MSG )reentrant;
int AniCallBack (  msg_data_t * MSG )reentrant;
#else
int BtnCallBack (  msg_data_t * MSG );
int BtnCallBack1 (  msg_data_t * MSG );
int PageCallBack (  msg_data_t * MSG );
int TextCallBack (  msg_data_t * MSG );
int ImageCallBack (  msg_data_t * MSG );
int ProgCallBack (  msg_data_t * MSG );
int ScrCallBack (  msg_data_t * MSG );
int AniCallBack (  msg_data_t * MSG );
#endif
void ZPrintCtrlBarPos(WORD ctrl_id, int offsetx, int offsety, U16 fcolor, U16 bcolor);



CONST UIPAGE HomePage = {
    HOMEPAGE_IDX, 2, 0, {0,0,1271,272},
    PageCallBack, 65535, 0, 0, 0, 0, 14
};



CONST UIPAGE SetupPage = {
    SETUPPAGE_IDX, 3, 0, {0,0,800,272},
    PageCallBack, 65535, 0, 0, 0, 14, 22
};



CONST UIIMAGE Image0 = {
    {WGUI_IMAGE}, 0, IMAGE0_IDX, {0,0,1271,272},
    0, 255, 0, {255,255,255,255}, ImageCallBack,
    0
};

CONST UIBUTTON btn1 = {
    {WGUI_BUTTON}, ZBTN_STYLE_IMAGE_DRAW, BTN1_IDX, {0,0,110,50},
    0, 255, 0, {255,255,255,255}, BtnCallBack,
    {1, 2, 65535, 65535 }, {65535,65535,65535,65535}
};

CONST UIBUTTON btn2 = {
    {WGUI_BUTTON}, ZBTN_STYLE_IMAGE_DRAW, BTN2_IDX, {0,221,110,50},
    0, 255, 0, {255,255,255,255}, BtnCallBack,
    {3, 4, 65535, 65535 }, {65535,65535,65535,65535}
};

CONST UIBUTTON btn3 = {
    {WGUI_BUTTON}, ZBTN_STYLE_IMAGE_DRAW, BTN3_IDX, {20,50,111,169},
    0, 255, 0, {255,255,255,255}, BtnCallBack,
    {5, 6, 65535, 65535 }, {65535,65535,65535,65535}
};

CONST UIBUTTON btn4 = {
    {WGUI_BUTTON}, ZBTN_STYLE_IMAGE_DRAW, BTN4_IDX, {132,50,111,169},
    0, 255, 0, {255,255,255,255}, BtnCallBack,
    {7, 8, 65535, 65535 }, {65535,65535,65535,65535}
};

CONST UIBUTTON btn5 = {
    {WGUI_BUTTON}, ZBTN_STYLE_IMAGE_DRAW, BTN5_IDX, {244,50,111,169},
    0, 255, 0, {255,255,255,255}, BtnCallBack,
    {9, 10, 65535, 65535 }, {65535,65535,65535,65535}
};

CONST UIBUTTON btn6 = {
    {WGUI_BUTTON}, ZBTN_STYLE_IMAGE_DRAW, BTN6_IDX, {356,50,111,169},
    0, 255, 0, {255,255,255,255}, BtnCallBack,
    {11, 12, 65535, 65535 }, {65535,65535,65535,65535}
};

CONST UIBUTTON btn7 = {
    {WGUI_BUTTON}, ZBTN_STYLE_IMAGE_DRAW, BTN7_IDX, {468,50,111,169},
    0, 255, 0, {255,255,255,255}, BtnCallBack,
    {13, 14, 65535, 65535 }, {65535,65535,65535,65535}
};

CONST UIBUTTON btn8 = {
    {WGUI_BUTTON}, ZBTN_STYLE_IMAGE_DRAW, BTN8_IDX, {580,50,111,169},
    0, 255, 0, {255,255,255,255}, BtnCallBack,
    {15, 16, 65535, 65535 }, {65535,65535,65535,65535}
};

CONST UIBUTTON btn9 = {
    {WGUI_BUTTON}, ZBTN_STYLE_IMAGE_DRAW, BTN9_IDX, {692,50,111,169},
    0, 255, 0, {255,255,255,255}, BtnCallBack,
    {17, 18, 65535, 65535 }, {65535,65535,65535,65535}
};

CONST UIBUTTON btn10 = {
    {WGUI_BUTTON}, ZBTN_STYLE_IMAGE_DRAW, BTN10_IDX, {804,50,111,169},
    0, 255, 0, {255,255,255,255}, BtnCallBack,
    {19, 20, 65535, 65535 }, {65535,65535,65535,65535}
};

CONST UIBUTTON btn11 = {
    {WGUI_BUTTON}, ZBTN_STYLE_IMAGE_DRAW, BTN11_IDX, {916,50,111,169},
    0, 255, 0, {255,255,255,255}, BtnCallBack,
    {21, 22, 65535, 65535 }, {65535,65535,65535,65535}
};

CONST UIBUTTON btn12 = {
    {WGUI_BUTTON}, ZBTN_STYLE_IMAGE_DRAW, BTN12_IDX, {1028,50,111,169},
    0, 255, 0, {255,255,255,255}, BtnCallBack,
    {23, 24, 65535, 65535 }, {65535,65535,65535,65535}
};

CONST UIBUTTON btn13 = {
    {WGUI_BUTTON}, ZBTN_STYLE_IMAGE_DRAW, BTN13_IDX, {1140,50,111,169},
    0, 255, 0, {255,255,255,255}, BtnCallBack,
    {25, 26, 65535, 65535 }, {65535,65535,65535,65535}
};

CONST UIIMAGE Image14 = {
    {WGUI_IMAGE}, 0, IMAGE14_IDX, {0,0,800,272},
    0, 255, 1, {255,255,255,255}, ImageCallBack,
    27
};

CONST UIBUTTON btn15 = {
    {WGUI_BUTTON}, ZBTN_STYLE_IMAGE_DRAW, BTN15_IDX, {0,0,110,50},
    0, 255, 1, {255,255,255,255}, BtnCallBack,
    {28, 29, 65535, 65535 }, {65535,65535,65535,65535}
};

CONST UIBUTTON btn16 = {
    {WGUI_BUTTON}, ZBTN_STYLE_IMAGE_DRAW, BTN16_IDX, {0,221,110,50},
    0, 255, 1, {255,255,255,255}, BtnCallBack,
    {30, 31, 65535, 65535 }, {65535,65535,65535,65535}
};

CONST UIBUTTON btn17 = {
    {WGUI_BUTTON}, ZBTN_STYLE_IMAGE_DRAW, BTN17_IDX, {40,76,108,145},
    0, 255, 1, {255,255,255,255}, BtnCallBack,
    {32, 33, 65535, 65535 }, {65535,65535,65535,65535}
};

CONST UIBUTTON btn18 = {
    {WGUI_BUTTON}, ZBTN_STYLE_IMAGE_DRAW, BTN18_IDX, {191,76,108,145},
    0, 255, 1, {255,255,255,255}, BtnCallBack,
    {34, 35, 65535, 65535 }, {65535,65535,65535,65535}
};

CONST UIBUTTON btn19 = {
    {WGUI_BUTTON}, ZBTN_STYLE_IMAGE_DRAW, BTN19_IDX, {347,76,108,145},
    0, 255, 1, {255,255,255,255}, BtnCallBack,
    {36, 37, 65535, 65535 }, {65535,65535,65535,65535}
};

CONST UIBUTTON btn20 = {
    {WGUI_BUTTON}, ZBTN_STYLE_IMAGE_DRAW, BTN20_IDX, {503,78,108,145},
    0, 255, 1, {255,255,255,255}, BtnCallBack,
    {38, 39, 65535, 65535 }, {65535,65535,65535,65535}
};

CONST UIBUTTON btn21 = {
    {WGUI_BUTTON}, ZBTN_STYLE_IMAGE_DRAW, BTN21_IDX, {657,76,108,145},
    0, 255, 1, {255,255,255,255}, BtnCallBack,
    {40, 41, 65535, 65535 }, {65535,65535,65535,65535}
};

void * CONST app_ui_ctrls[] = {(void *)&Image0, (void *)&btn1, (void *)&btn2, (void *)&btn3, (void *)&btn4, /*00-04*/(void *)&btn5, (void *)&btn6, (void *)&btn7, (void *)&btn8, (void *)&btn9, /*05-09*/(void *)&btn10, (void *)&btn11, (void *)&btn12, (void *)&btn13, (void *)&Image14, /*10-14*/(void *)&btn15, 
                               (void *)&btn16, (void *)&btn17, (void *)&btn18, (void *)&btn19, /*15-19*/(void *)&btn20, (void *)&btn21};

void * CONST app_ui_pages[] = {(void *)&HomePage, (void *)&SetupPage};



void DrawButtonControl(LPUIPBUTTON pButton)
{
    U8 status=255;
    ZGetCtrlParam(pButton->CtrlID, ZVAL_VALUE, &status);
    switch(pButton->dExtStyle)
    {
        case ZBTN_STYLE_IMAGE_BFLASH:
        {
            if((U8)status == (U8)ZCTRL_UNFOCUS)
            {
                TWLL_Icon_Create(pButton->hImageID[ZCTRL_UNFOCUS],IconShowOnCanvas);
                if(pButton->textID[ZCTRL_UNFOCUS] != (int)65535)
                {
                    TWLL_String_Create(pButton->textID[ZCTRL_UNFOCUS], StringShowOnCanvas);
                }
            }
            else
            {
            LPUIPAGE lppage;
                if(InvalidPageNumber(pButton))
                {
                    return ;
                }
                lppage = app_ui_pages[pButton->hParent];
                TWLL_Icon_Create(pButton->hImageID[ZCTRL_UNFOCUS],IconShowOnCanvas);
                //ZApp_DrawIcon_Scratch_page(SCRATCH_CATCH_BUFFER_ID, pButton->hImageID[ZCTRL_UNFOCUS], IconShowOnCanvas);
            }
        }
        break;
        case ZBTN_STYLE_IMAGE_DRAW:
            ZSet_Draw_Update(1);
            if((U16)65535 != pButton->hImageID[status])
                TWLL_Icon_Create(pButton->hImageID[status],IconShowOnCanvas);
            else if((U16)65535 != pButton->hImageID[1])
                TWLL_Icon_Create(pButton->hImageID[1],IconShowOnCanvas);
            if(pButton->textID[status] != (int)65535)
            {
                TWLL_String_Create(pButton->textID[status], StringShowOnCanvas);
            }
            else if(pButton->textID[1] != (int)65535)
            {
                TWLL_String_Create(pButton->textID[1], StringShowOnCanvas);
            }
            //ZApp_DrawIcon_Scratch_page(SCRATCH_CATCH_BUFFER_ID, pButton->hImageID[status], IconShowOnCanvas);
        break;
        case ZBTN_STYLE_TXT_DRAW:
        break;
        case ZBTN_STYLE_TXT_BFLASH:
        break;
    }
}


void DrawTextControl(LPUITEXT pText)
{
    DrawFont(pText->textID);
}


void DrawImageControl(LPUIIMAGE pImage)
{
    DrawImage(pImage->hImage);
}


void DrawProgressControl(LPUIPROGRESS lppro)
{
    U8 value;
    WORD width, height, new_pos,ii, in_pos;
    PROGRESSBARINFO * pinfo;
    pinfo = &lppro->info;
    ZGetCtrlParam(lppro->CtrlID, ZVAL_VALUE, &value);
	TWLL_Get_ImageSize(TWLL_Get_ImageID(lppro->hImage[ZPRO_IMAGE_TRACK], IconShowOnCanvas),&width,&height);
    TWLL_Icon_Create(lppro->hImage[ZPRO_IMAGE_BACKGROUND], IconShowOnCanvas);
    //ZApp_DrawIcon_Scratch_page(SCRATCH_CATCH_BUFFER_ID, lppro->hImage[ZPRO_IMAGE_BACKGROUND], IconShowOnCanvas);
    pinfo = &lppro->info;
    if(pinfo->max_value == pinfo->min_value)
    {
        return;
    }
    switch(lppro->dExtStyle)
    {
        case ZPRO_DH_LR:
            new_pos = (value-pinfo->min_value)*lppro->rect.width/(pinfo->max_value-pinfo->min_value);
            if(new_pos != 0)
            {
                in_pos = width*5/4;
                new_pos = new_pos/in_pos;
                for(ii=0;ii<new_pos;ii++)
                {
                    ZApp_Draw_Image_XY(lppro->hImage[ZPRO_IMAGE_TRACK], IconShowOnCanvas, lppro->rect.left+in_pos*ii, lppro->rect.top);
                }
            }
        break;
        case ZPRO_DH_RL:
            //new_pos = (pinfo->max_value-pinfo->cur_value)*width/(pinfo->max_value-pinfo->min_value);
            //TWLL_Icon_Create_XYWH(lppro->hImage[ZPRO_IMAGE_TRACK], IconShowOnCanvas, new_pos, 0, width-new_pos, height);
        break;
        case ZPRO_DV_TB:
            //new_pos = (pinfo->cur_value-pinfo->min_value)*height/(pinfo->max_value-pinfo->min_value);
            //TWLL_Icon_Create_XYWH(lppro->hImage[ZPRO_IMAGE_TRACK], IconShowOnCanvas, 0, 0, width, new_pos);
        break;
        case ZPRO_DV_BT:
            new_pos = (value-pinfo->min_value)*lppro->rect.height/(pinfo->max_value-pinfo->min_value);
            if(new_pos != 0)
            {
                in_pos = height*5/4;
                new_pos = new_pos/in_pos;
                for(ii=0;ii<new_pos;ii++)
                {
                    ZApp_Draw_Image_XY(lppro->hImage[ZPRO_IMAGE_TRACK], IconShowOnCanvas, lppro->rect.left, lppro->rect.top+lppro->rect.height-height - in_pos*ii);
                }
            }
        break;
    }
    //ZPrintCtrlBarPos(lppro->CtrlID, 0, 0, (U16)255, (U16)0);
}


void DrawScrollbarControl(LPUISCROLLBAR pScrollbar)
{
    U8 value,status;
    U16 px,py,ph,tv, hh, ii, num,width, height;
    extern CONST BITMAP SystemIcon[];
    extern CONST UIControlIcon_struct IconShowOnCanvas[];
    ZGetCtrlParam(pScrollbar->CtrlID, ZVAL_VALUE, &value);
    status = value&0x80;
    value = value&0x7f;
    TWLL_Icon_Create(pScrollbar->hImage[0], IconShowOnCanvas);
	TWLL_Get_ImageSize(TWLL_Get_ImageID(pScrollbar->hImage[1],IconShowOnCanvas),&width,&height);
    //ZApp_DrawIcon_Scratch_page(SCRATCH_CATCH_BUFFER_ID, pScrollbar->hImage[0], IconShowOnCanvas);
    if(pScrollbar->dExtStyle == ZSCR_HD)
    {
        px = pScrollbar->sScrollbarinfo.rcTrackBack.left;
        py = pScrollbar->sScrollbarinfo.rcTrackBack.top;
        tv = pScrollbar->sScrollbarinfo.rcTrackBack.height*5/16;
       ph=width;
        hh = ph*5/4;
        num = ((value-pScrollbar->sScrollbarValue.Min)*pScrollbar->sScrollbarinfo.rcTrackBack.width+hh-1)/(pScrollbar->sScrollbarValue.Max-pScrollbar->sScrollbarValue.Min)/hh;
        if(num > 0)
        {
            for(ii=0;ii<num;ii++)
            {
                ZApp_Draw_Image_XY(pScrollbar->hImage[1], IconShowOnCanvas, px+hh*ii, py+tv);
            }
            ZApp_Draw_Image_XY(pScrollbar->hImage[2], IconShowOnCanvas, px+hh*num, py);
        }
        else
        {
            ZApp_Draw_Image_XY(pScrollbar->hImage[2], IconShowOnCanvas, px, py);
        }
    }
    else
    {
        px = pScrollbar->sScrollbarinfo.rcTrackBack.left;
        py = pScrollbar->sScrollbarinfo.rcTrackBack.top;
       ph=height;
        hh = ph*5/4;
        num = ((value-pScrollbar->sScrollbarValue.Min)*pScrollbar->sScrollbarinfo.rcTrackBack.height+hh-1)/(pScrollbar->sScrollbarValue.Max-pScrollbar->sScrollbarValue.Min)/hh;
        tv = py+pScrollbar->sScrollbarinfo.rcTrackBack.height;
        if(num > 0)
        {
            for(ii=0;ii<num;ii++)
            {
                ZApp_Draw_Image_XY(pScrollbar->hImage[1], IconShowOnCanvas, px, tv-hh*ii);
            }
            ZApp_Draw_Image_XY(pScrollbar->hImage[2], IconShowOnCanvas, pScrollbar->sScrollbarinfo.rcTrack.left, tv-(num-1)*hh-6);
        }
        else
        {
	TWLL_Get_ImageSize(TWLL_Get_ImageID(pScrollbar->hImage[2],IconShowOnCanvas),&width,&height);
	ZApp_Draw_Image_XY(pScrollbar->hImage[2], IconShowOnCanvas,pScrollbar->sScrollbarinfo.rcTrack.left,pScrollbar->sScrollbarinfo.rcDown.top-height);
        }
    }
    if(status)
    {
        status = 1;
    }
    else
    {
        status = 0;
    }
    TWLL_Icon_Create(pScrollbar->hImage[3+status], IconShowOnCanvas);
    TWLL_Icon_Create(pScrollbar->hImage[5+status], IconShowOnCanvas);
    //ZPrintCtrlBarPos(pScrollbar->CtrlID, 0, 0, (U16)255, (U16)0);
}



void DrawImage(U16 imageID)
{
    if(65535 != imageID)
        TWLL_Icon_Create(imageID, IconShowOnCanvas);
}



void DrawFont(int textID)
{
    TWLL_String_Create(textID, StringShowOnCanvas);
}


U8 getCanvasHardwareid(U8 cid)
{
   return User_Canvas[cid].Display_Layer;
}

#ifdef Used_BlockTransfer
U8 BlockTransStatus=0;
#endif

U16 ZCreatePage(U8 page_id)
{
    LPUIPAGE lppage;
    WORD CanNum;
    U8 PagerID;
    PagerID = page_id;
    lppage = app_ui_pages[PagerID];
    CanNum = lppage->nWinno;
    if(lppage->subPage == 0)
    {
#ifdef Used_BlockTransfer
        UIDesTroyAllActivePage(User_Canvas[CanNum].Bits_Per_Pixel);
#else
        UIDesTroyAllActivePage();
#endif
    }
    else if(255 == isValidLofiCanvasID(CanNum))
    {
        return 65535;
    }
#ifndef Used_BlockTransfer
    TWLL_Canvas_OnOff(CanNum,0);
#endif
    InitPageValue(PagerID);
#ifndef Used_BlockTransfer
    TWLL_Canvas_Init(&User_Canvas[CanNum]);
#endif
#ifdef Used_BlockTransfer
    BlockTransStatus=1;
#endif
    ZApp_Clear_Canvas_Page(lppage->nWinno,lppage->bcolor);
    UICreateOnePage(PagerID, lppage);
#ifdef Used_BlockTransfer
    BlockTransStatus=0;
    TWLL_Canvas_Init(&User_Canvas[CanNum]);
    TWLL_WaitDisplayBlank(1);
    if(User_Canvas[CanNum].Bits_Per_Pixel==16)
        TWLL_BlockTransfer (User_Canvas[CanNum].Mem_X,
            User_Canvas[CanNum].Mem_Y+272, User_Canvas[CanNum].width,
            User_Canvas[CanNum].height, User_Canvas[CanNum].Mem_X,
            User_Canvas[CanNum].Mem_Y, User_Canvas[CanNum].Bits_Per_Pixel);
#endif
    TWLL_Canvas_OnOff(CanNum,1);
    return 0;
}


U16 ZReturnParentPage(void)
{
    BYTE CanNum;
    CanNum = ZGetCSubPageCanvasID();
    TWLL_Canvas_OnOff(CanNum,0);
    ZUIDestroyActiveSubPage();
    ZActiveSeqSubPage();
    return 0;
}


void ZInit_App(int flag)
{
    /* Initialize the envireonment valuable */
    ZGuiEnvInit();

    /* initialize the  reg ctrls*/
    ZRegisterCtrls((void**)&app_ui_ctrls[0], sizeof(app_ui_ctrls)/sizeof(void*));
    /* initialize the  reg pages*/
    ZRegisterPages((void**)&app_ui_pages[0], sizeof(app_ui_pages)/sizeof(void*));

    if(flag>=0)
    {
        ZSet_Draw_Update(1);
        ZCreatePage(flag); /* Start desktop */    
        ZSet_Draw_Update(0);
    }
}
//You need to define USED_USER_BTN_CODE in the front of file if you use yourself callback functions

#ifndef USED_USER_BTN_CODE

// Pager reply function for touch moving and move end message:
/*
 Function name: OnHomePageTouchMoving
 Page name:     HomePage
 Page id:       0
 */
void OnHomePageTouchMoving(U16 px, U16 py)
{
}

/*
 Function name: OnHomePageTouchMoved
 Page name:     HomePage
 Page id:       0
 */
void OnHomePageTouchMoved(U16 px, U16 py)
{
}
/*
 Function name: OnSetupPageTouchMoving
 Page name:     SetupPage
 Page id:       1
 */
void OnSetupPageTouchMoving(U16 px, U16 py)
{
}

/*
 Function name: OnSetupPageTouchMoved
 Page name:     SetupPage
 Page id:       1
 */
void OnSetupPageTouchMoved(U16 px, U16 py)
{
}

// Pager reply function declaration end.


// Button reply function declaration start:

/*
 Function name: Onbtn1Click
 Ctrl name:     btn1
 Ctrl id:       1
 */
void Onbtn1Click(void)
{
    //Please add your button click event here
}

/*
 Function name: Onbtn2Click
 Ctrl name:     btn2
 Ctrl id:       2
 */
void Onbtn2Click(void)
{
    //Please add your button click event here
}

/*
 Function name: Onbtn3Click
 Ctrl name:     btn3
 Ctrl id:       3
 */
void Onbtn3Click(void)
{
    //Please add your button click event here
}

/*
 Function name: Onbtn4Click
 Ctrl name:     btn4
 Ctrl id:       4
 */
void Onbtn4Click(void)
{
    //Please add your button click event here
}

/*
 Function name: Onbtn5Click
 Ctrl name:     btn5
 Ctrl id:       5
 */
void Onbtn5Click(void)
{
    //Please add your button click event here
}

/*
 Function name: Onbtn6Click
 Ctrl name:     btn6
 Ctrl id:       6
 */
void Onbtn6Click(void)
{
    //Please add your button click event here
}

/*
 Function name: Onbtn7Click
 Ctrl name:     btn7
 Ctrl id:       7
 */
void Onbtn7Click(void)
{
    //Please add your button click event here
}

/*
 Function name: Onbtn8Click
 Ctrl name:     btn8
 Ctrl id:       8
 */
void Onbtn8Click(void)
{
    //Please add your button click event here
}

/*
 Function name: Onbtn9Click
 Ctrl name:     btn9
 Ctrl id:       9
 */
void Onbtn9Click(void)
{
    //Please add your button click event here
}

/*
 Function name: Onbtn10Click
 Ctrl name:     btn10
 Ctrl id:       10
 */
void Onbtn10Click(void)
{
    //Please add your button click event here
}

/*
 Function name: Onbtn11Click
 Ctrl name:     btn11
 Ctrl id:       11
 */
void Onbtn11Click(void)
{
    //Please add your button click event here
}

/*
 Function name: Onbtn12Click
 Ctrl name:     btn12
 Ctrl id:       12
 */
void Onbtn12Click(void)
{
    //Please add your button click event here
}

/*
 Function name: Onbtn13Click
 Ctrl name:     btn13
 Ctrl id:       13
 */
void Onbtn13Click(void)
{
    //Please add your button click event here
}

/*
 Function name: Onbtn15Click
 Ctrl name:     btn15
 Ctrl id:       15
 */
void Onbtn15Click(void)
{
    //Please add your button click event here
}

/*
 Function name: Onbtn16Click
 Ctrl name:     btn16
 Ctrl id:       16
 */
void Onbtn16Click(void)
{
    //Please add your button click event here
}

/*
 Function name: Onbtn17Click
 Ctrl name:     btn17
 Ctrl id:       17
 */
void Onbtn17Click(void)
{
    //Please add your button click event here
}

/*
 Function name: Onbtn18Click
 Ctrl name:     btn18
 Ctrl id:       18
 */
void Onbtn18Click(void)
{
    //Please add your button click event here
}

/*
 Function name: Onbtn19Click
 Ctrl name:     btn19
 Ctrl id:       19
 */
void Onbtn19Click(void)
{
    //Please add your button click event here
}

/*
 Function name: Onbtn20Click
 Ctrl name:     btn20
 Ctrl id:       20
 */
void Onbtn20Click(void)
{
    //Please add your button click event here
}

/*
 Function name: Onbtn21Click
 Ctrl name:     btn21
 Ctrl id:       21
 */
void Onbtn21Click(void)
{
    //Please add your button click event here
}


// Button reply function declaration end.


void InitCtrlValue(HUICTRL hCtrl)
{
    switch(hCtrl)
    {
    default:
    break;
    }
}

void InitPageValue(HUICTRL page_id)
{
    switch(page_id)
    {
    default:
    break;
    }
}

#endif

void *GetCtrlHandle(HUICTRL hCtrl)
{
    LPBASICWIN lpbasic;
    if(hCtrl >= sizeof(app_ui_ctrls)/sizeof(void*))
        lpbasic = NULL;
    else
        lpbasic = (LPBASICWIN)app_ui_ctrls[hCtrl];
    return lpbasic;
}

void ZUIRedrawCtrl(U16 ctrl_id)
{
    LPBASICWIN lpbasic;
    if(zIsActiveCtrl(ctrl_id) == 0)
    {
        lpbasic = (LPBASICWIN)app_ui_ctrls[ctrl_id];
        if(lpbasic)
        {
            switch(lpbasic->type[0])
            {
                case WGUI_BUTTON:
                    DrawButtonControl((LPUIPBUTTON)lpbasic);
                break;
                case WGUI_SCROLLBAR:
                    DrawScrollbarControl((LPUISCROLLBAR)lpbasic);
                break;
                case WGUI_TEXT:
                    DrawTextControl((LPUITEXT)lpbasic);
                break;
                case WGUI_IMAGE:
                    DrawImageControl((LPUIIMAGE)lpbasic);
                break;
                case WGUI_PROGRESS:
                    DrawProgressControl((LPUIPROGRESS)lpbasic);
                break;
            }
        }
    }
}

void ZUIRedrawPage(U8 page_id)
{
    LPBASICWIN lpbasic;
    int ii;
    if(zIsActivePage(page_id) == 0)
    {
        for(ii=0;ii<sizeof(app_ui_ctrls)/sizeof(void*);ii++)
        {
            lpbasic = (LPBASICWIN)app_ui_ctrls[ii];
            if(lpbasic->hParent == page_id)
            {
                switch(lpbasic->type[0])
                {
                case WGUI_BUTTON:
                    DrawButtonControl((LPUIPBUTTON)lpbasic);
                break;
                case WGUI_SCROLLBAR:
                    DrawScrollbarControl((LPUISCROLLBAR)lpbasic);
                break;
                case WGUI_TEXT:
                    DrawTextControl((LPUITEXT)lpbasic);
                break;
                case WGUI_IMAGE:
                    DrawImageControl((LPUIIMAGE)lpbasic);
                break;
                case WGUI_PROGRESS:
                    DrawProgressControl((LPUIPROGRESS)lpbasic);
                break;
                }
            }
        }
    }
}

void ProcessCtrlofPage(U16 page_id)
{
    int ii;
    LPBASICWIN lpbasic;
    for(ii=0;ii<sizeof(app_ui_ctrls)/sizeof(void*);ii++)
    {
        lpbasic = (LPBASICWIN)app_ui_ctrls[ii];
        if(lpbasic->hParent == page_id)
        {
            //here you can add your action;
        }
    }
}

void *GetPageHandle(HUICTRL page_id)
{
    void * lppage;
    if(page_id >= sizeof(app_ui_pages)/sizeof(void*))
        lppage = NULL;
    else
        lppage = app_ui_pages[page_id];
    return lppage;
}

#ifdef Keil_C51
int BtnCallBack ( msg_data_t * MSG )reentrant
#else
int BtnCallBack ( msg_data_t * MSG )
#endif
{
    LPUIPBUTTON  lpBtn;
    HUICTRL hCtrl;
    U8 uMsg;
    WORD param1;
    WORD px, py;

    hCtrl=MSG->mitem;
    uMsg=MSG->mtype;
    param1=MSG->mparam;
    px = MSG->mparal;
    py = MSG->mparan;	

    lpBtn = app_ui_ctrls[hCtrl];
    hCtrl=MSG->mitem;

    if(lpBtn == 0)
    {
        return -1;
    }
    if(lpBtn ->type[0] != WGUI_BUTTON)
    {
        return -1;
    }
    switch(uMsg)
    {
    case MWUI_PUBLIC:
        switch(param1)
        {
            case MWUI_UPDATE:
            break;
            case MWUI_CREATE:
            {
                LPUIPAGE lppage;
                if(lpBtn->hParent>= sizeof(app_ui_pages)/sizeof(void*))
                {
                    return -1;
                }
                lppage = (LPUIPAGE)(app_ui_pages[lpBtn->hParent]);
                if(lppage == 0)
                {
                    return -1;
                }
                if((U16)hCtrl == (U16)(lppage->active_ctrl_id))
                {
                    ZSetCtrlParam(hCtrl, ZVAL_VALUE, ZCTRL_FOCUS);
                }
                else
                {
                    ZSetCtrlParam(hCtrl, ZVAL_VALUE, ZCTRL_UNFOCUS);
                }
                InitCtrlValue(hCtrl);
                DrawButtonControl(lpBtn);
            }
            break;
            case MWUI_DESTROY:
                TWLL_Icon_Remove(0, &IconShowOnCanvas[lpBtn->hImageID[1]]);
            break;
            case MWUI_CLEAR:
                if(lpBtn ->dExtStyle == ZBTN_STYLE_IMAGE_DRAW)
                {
                    U8 status=255;
                    ZGetCtrlParam(lpBtn ->CtrlID, ZVAL_VALUE, &status);
                    if(ZCTRL_FOCUS == status  && lpBtn->hImageID[0] != 65535)
                    {
                        TWLL_Icon_Remove(0, &IconShowOnCanvas[lpBtn->hImageID[0]]);
                    }
                    else
                    {
                        if(lpBtn->hImageID[1] != (int)65535)
                            TWLL_Icon_Remove(0, &IconShowOnCanvas[lpBtn->hImageID[1]]);
                    }
                }
            break;
        }
    break;
    case MWUI_KEYPRESS:
        switch(param1)
        {
        case KEYUI_MENU:
            switch(hCtrl)
            {
            case 1:
                Onbtn1Click();
            break;
            case 2:
                Onbtn2Click();
            break;
            case 3:
                Onbtn3Click();
            break;
            case 4:
                Onbtn4Click();
            break;
            case 5:
                Onbtn5Click();
            break;
            case 6:
                Onbtn6Click();
            break;
            case 7:
                Onbtn7Click();
            break;
            case 8:
                Onbtn8Click();
            break;
            case 9:
                Onbtn9Click();
            break;
            case 10:
                Onbtn10Click();
            break;
            case 11:
                Onbtn11Click();
            break;
            case 12:
                Onbtn12Click();
            break;
            case 13:
                Onbtn13Click();
            break;
            case 15:
                Onbtn15Click();
            break;
            case 16:
                Onbtn16Click();
            break;
            case 17:
                Onbtn17Click();
            break;
            case 18:
                Onbtn18Click();
            break;
            case 19:
                Onbtn19Click();
            break;
            case 20:
                Onbtn20Click();
            break;
            case 21:
                Onbtn21Click();
            break;
            default:
            break;
            }
        break;
        }
    break;
    case MWUI_NOTIFY:
    break; 
    case MWUI_TOUCH:
        switch(param1)
        {
        case KWUI_TOUCH_UP:
        {
            LPBASICWIN lpbasic = app_ui_ctrls[hCtrl];
            ZInvalidateMouseFocus(lpbasic->hParent);
            switch(hCtrl)
            {
            case 1:
                Onbtn1Click();
            break;
            case 2:
                Onbtn2Click();
            break;
            case 3:
                Onbtn3Click();
            break;
            case 4:
                Onbtn4Click();
            break;
            case 5:
                Onbtn5Click();
            break;
            case 6:
                Onbtn6Click();
            break;
            case 7:
                Onbtn7Click();
            break;
            case 8:
                Onbtn8Click();
            break;
            case 9:
                Onbtn9Click();
            break;
            case 10:
                Onbtn10Click();
            break;
            case 11:
                Onbtn11Click();
            break;
            case 12:
                Onbtn12Click();
            break;
            case 13:
                Onbtn13Click();
            break;
            case 15:
                Onbtn15Click();
            break;
            case 16:
                Onbtn16Click();
            break;
            case 17:
                Onbtn17Click();
            break;
            case 18:
                Onbtn18Click();
            break;
            case 19:
                Onbtn19Click();
            break;
            case 20:
                Onbtn20Click();
            break;
            case 21:
                Onbtn21Click();
            break;
            default:
            break;
            }
        }
        break;
        case KWUI_TOUCH_DOWN:
            {
//                ZSetCtrlFocus(hCtrl, 1); //eamon.fang
            }
        break;
        case KWUI_TOUCH_NORMAL:   //Erek added for three state
            {
                ZSetCtrlFocus(hCtrl, 0);
            }
        break;
        case KWUI_TOUCH_DISABLE:   //Erek added for three state
            {
                ZSetCtrlFocus(hCtrl, 2);
            }
        break;
        case KWUI_TOUCH_SUBPAGE:   //Erek added for three state
            {
                ZSetCtrlFocus(hCtrl, 3);
            }
        break;
		// eamon.fang
        case KWUI_TOUCH_MOVING:
        switch (hCtrl)
        {
			case 17:
			OnButton17TouchMoving(lpBtn, px, py);
			break;
			case 18:
//			OnHomePageTouchMoving(px, py);
			break;
			case 19:
//			OnHomePageTouchMoving(px, py);
			break;
			case 20:
//			OnHomePageTouchMoving(px, py);
			break;
			case 21:
//			OnHomePageTouchMoving(px, py);
			break;
        }
        break;
        case KWUI_TOUCH_MOVE_END:
        switch (hCtrl)
        {
            case 17:
				OnButton17TouchMoved(px, py);
            break;
			case 18:
//			OnHomePageTouchMoving(px, py);
			break;
			case 19:
//			OnHomePageTouchMoving(px, py);
			break;
			case 20:
//			OnHomePageTouchMoving(px, py);
			break;
			case 21:
//			OnHomePageTouchMoving(px, py);
			break;
        }
        break;		
        }
    break; 
    }
    return 0;
}


#ifdef Keil_C51
int PageCallBack ( msg_data_t * MSG )reentrant
#else
int PageCallBack ( msg_data_t * MSG )
#endif
{
    HUICTRL hCtrl;
    U8 uMsg;
    WORD param1,px,py;
    hCtrl=MSG->mitem;
    uMsg=MSG->mtype;
    param1=MSG->mparam;
    px=MSG->mparal;
    py=MSG->mparan;
    switch(uMsg)
    {
        case MWUI_PUBLIC:
            switch(param1)
            {
                case MWUI_UPDATE:
                break;
            }
        break;
        case MWUI_KEYPRESS:
            switch(param1)
            {
                case KEYUI_TOUCH:
                break;
                case KEYUI_EXIT:
                case KEYUI_LEFT:
                case KEYUI_RT:
                break;
            }
        break;
        case MWUI_NOTIFY:
        break;
        case MWUI_TOUCH:
            switch(param1)
            {
                case KWUI_TOUCH_MOVING:
                switch(hCtrl)
                {
                    case HOMEPAGE_IDX:
                        OnHomePageTouchMoving(px, py);
                    break;
                    case SETUPPAGE_IDX:
                        OnSetupPageTouchMoving(px, py);
                    break;
                }
                break;
                case KWUI_TOUCH_MOVE_END:
                switch(hCtrl)
                {
                    case HOMEPAGE_IDX:
                        OnHomePageTouchMoved(px, py);
                    break;
                    case SETUPPAGE_IDX:
                        OnSetupPageTouchMoved(px, py);
                    break;
                }
                break;
            }
        break;
    }
    return 0;
}


#ifdef Keil_C51
int TextCallBack ( msg_data_t * MSG )reentrant
#else
int TextCallBack ( msg_data_t * MSG )
#endif
{
    LPUITEXT lptext;

    HUICTRL hCtrl;
    U8 uMsg;
    WORD param1;
    hCtrl=MSG->mitem;
    uMsg=MSG->mtype;
    param1=MSG->mparam;
    lptext = app_ui_ctrls[hCtrl];
    if(lptext->type[0] != WGUI_TEXT)
    {
        return 0;
    }

    switch(uMsg)
    {
        case MWUI_PUBLIC:
            switch(param1)
            {
                case MWUI_UPDATE:
                break;
                case MWUI_CREATE:
                    InitCtrlValue(hCtrl);
                    DrawTextControl(lptext);
                break;
                case MWUI_DESTROY:
                    TWLL_String_Remove(0, &StringShowOnCanvas[lptext->textID]);
                break;
            }
        break;
        case MWUI_KEYPRESS:
            switch(param1)
            {

                case KEYUI_MENU:
                break;
            }
        break;
        case MWUI_NOTIFY:
        break; 
    }
    return 0;
}



#ifdef Keil_C51
int ImageCallBack ( msg_data_t * MSG )reentrant
#else
int ImageCallBack ( msg_data_t * MSG )
#endif
{
    LPUIIMAGE lpImage;
    HUICTRL hCtrl;
    U8 uMsg;
    WORD param1;
    hCtrl=MSG->mitem;
    uMsg=MSG->mtype;
    param1=MSG->mparam;
    lpImage = app_ui_ctrls[hCtrl];
    switch(uMsg)
    {
        case MWUI_PUBLIC:
            switch(param1)
            {
                case MWUI_UPDATE:
                break;
                case MWUI_CREATE:
                    InitCtrlValue(hCtrl);
                    DrawImageControl(lpImage);
                break;
                case MWUI_DESTROY:
                    TWLL_Icon_Remove(0, &IconShowOnCanvas[lpImage->hImage]);
                break;
            }
        break;
        case MWUI_KEYPRESS:
            switch(param1)
            {
                case KEYUI_MENU:
                break;
            }
        break;
        case MWUI_NOTIFY:
        break; 
    }
    return 0;
}


U16 ZGetImageID(U16 id, UIControlIcon_struct *icon)
{
    return icon[id].BMP_ID;
}


U8 ZGetImageWH(U16 imageid, U16 * pw, U16 *ph)
{
    BITMAP bitmap;
    extern CONST BITMAP SystemIcon[];
    bitmap = SystemIcon[imageid];
    *pw = bitmap.width;
    *ph = bitmap.height;
    return (U8)APP_OKEY;
}
#ifdef Keil_C51
int ProgCallBack ( msg_data_t * MSG )reentrant
#else
int ProgCallBack ( msg_data_t * MSG )
#endif
{
    LPUIPROGRESS lppro;
    int value;
    U8 val;
    short signal;
    HUICTRL hCtrl;
    U8 uMsg;
    WORD param1;
    WORD param2;
    WORD param3;
    hCtrl=MSG->mitem;
    uMsg=MSG->mtype;
    param1=MSG->mparam;
    param2=MSG->mparal;
    param3=MSG->mparan;
    lppro = app_ui_ctrls[hCtrl];
    signal = param3;
    if(lppro->type[0] != (U8)WGUI_PROGRESS)
    {
        return APP_ERROR;
    }
    switch(uMsg)
    {
        case MWUI_PUBLIC:
            switch(param1)
            {
                case MWUI_UPDATE:
                    ZGetCtrlParam(hCtrl, ZVAL_VALUE, &val);
                    if(param2 == ZVAL_TYPE_ABSOLUTE)
                    {
                        value = signal*lppro->info.step;
                    }
                    else 
                    {
                        value = val;
                        value += signal*lppro->info.step;
                    }
                    if(value< lppro->info.min_value)val = lppro->info.min_value;
                    else if(value > lppro->info.max_value )val = lppro->info.max_value;
                    else val = value;
                    ZSetCtrlParam(hCtrl, ZVAL_VALUE, val);
                    DrawProgressControl(lppro);
                    return 0;
                break;
                case MWUI_CREATE:
                    ZSetCtrlParam(hCtrl, ZVAL_VALUE, lppro->info.cur_value);
                    InitCtrlValue(hCtrl);
                    DrawProgressControl(lppro);
                break;
            }
        break;
        case MWUI_KEYPRESS:
        break;
        case MWUI_NOTIFY:
        break; 
    }
    return 0;
}


int ZIsPointInScrRect(int px, int py, LPUIRECT prect, U16 ctrl_id)
{
    UIRECT rect;
    U16 psx, psy;
    LPUIPAGE lpPage;
    LPBASICWIN lpbasic;

    lpbasic = (LPBASICWIN)app_ui_ctrls[ctrl_id];
    lpPage = app_ui_pages[lpbasic->hParent];
    psx = (U16)(User_Canvas[lpPage->nWinno].Screen_X);
    psy = (U16)(User_Canvas[lpPage->nWinno].Screen_Y);
    rect.left = psx + prect->left;
    rect.top  = psy + prect->top;
    rect.width = prect->width;
    rect.height = prect->height;
    return(ZIsPointInRect(px, py, &rect));
}

U8 ZGetCtrlValue(WORD ctrl_id)
{
    LPBASICWIN lpbasic;
    U8 new_pos,pos;

    if(ctrl_id>= sizeof(app_ui_ctrls)/sizeof(void*))
    {
        return (U8)255;;
    }

    lpbasic = app_ui_ctrls[ctrl_id];
    if(lpbasic == 0)
    {
        return (U8)255;;
    }
    if(lpbasic->type[0] == (U8)WGUI_SCROLLBAR)
    {
        ZGetCtrlParam(ctrl_id, ZVAL_VALUE, &new_pos);
        pos = new_pos&0x7f;
    }
    else if(lpbasic->type[0] == (U8)WGUI_PROGRESS)
    {
        ZGetCtrlParam(ctrl_id, ZVAL_VALUE, &pos);
    }
    else
    {
        return (U8)255;
    }
    return pos;
}

U8 ZSetCtrlValue(WORD ctrl_id, U8 value, U8 mode)
{
    LPBASICWIN lpbasic;
    U8 new_pos,sta=0, ret=255;
    int pos;
    if(ctrl_id>= sizeof(app_ui_ctrls)/sizeof(void*))
    {
        return (U8)255;
    }

    lpbasic = app_ui_ctrls[ctrl_id];

    if(lpbasic == 0)
    {
        return (U8)255;
    }
    if(lpbasic->type[0] == (U8)WGUI_SCROLLBAR)
    {
        LPUISCROLLBAR lpscr;
        lpscr = (LPUISCROLLBAR)lpbasic;
        if(mode == 0)
        {
            pos = value;
        }
        else
        {
            ZGetCtrlParam(ctrl_id, ZVAL_VALUE, &new_pos);
            sta = new_pos&0x80;
            pos = new_pos&0x7f;
            pos += value*lpscr->sScrollbarValue.step;
        }
        if(pos > lpscr->sScrollbarValue.Max)pos = lpscr->sScrollbarValue.Max;
        else if(pos < lpscr->sScrollbarValue.Min)pos = lpscr->sScrollbarValue.Min;
        ZSetCtrlParam(ctrl_id, ZVAL_VALUE, pos|sta);
        ret = 0;
    }
    else if(lpbasic->type[0] == (U8)WGUI_PROGRESS)
    {
        new_pos = value;
        ZSetCtrlParam(ctrl_id, ZVAL_VALUE, new_pos);
        ret = 0;
    }
    return ret;
}
void * ZGetInfoPtr(WORD ctrl_id)
{
    void * ret_ptr;
    LPUISCROLLBAR lpscr;
    LPUIANIMATOR lpani;
    LPUIPROGRESS lppro;
    if(ctrl_id>= sizeof(app_ui_ctrls)/sizeof(void*))
    {
        return NULL;
    }
    lpscr = (LPUISCROLLBAR)app_ui_ctrls[ctrl_id];

    if(lpscr == 0)
    {
        return NULL;;
    }
    ret_ptr = NULL;
    if(lpscr->type[0] == (U8)WGUI_SCROLLBAR)
    {
        ret_ptr = &lpscr->sScrollbarValue;
    }
    if(lpscr->type[0] == (U8)WGUI_PROGRESS)
    {
        lppro = (LPUIPROGRESS)lpscr;
        ret_ptr = &lppro->info;
    }
    if(lpscr->type[0] == (U8)WGUI_ANIMATER)
    {
        lpani = (LPUIANIMATOR)lpscr;
        ret_ptr = &lpani->sAnimateInfo;
    }
    return ret_ptr;
}
void ZPrintCtrlBarPos(WORD ctrl_id, int offsetx, int offsety, U16 fcolor, U16 bcolor)
{
    LPUISCROLLBAR lpscr;
    U8 cur_val;
    U16 px,py;
    LPUIPAGE lppage;
    if(ctrl_id>= sizeof(app_ui_ctrls)/sizeof(void*))
    {
        return;
    }
    lpscr = (LPUISCROLLBAR)app_ui_ctrls[ctrl_id];

    if(lpscr == 0)
    {
        return;
    }
    if(lpscr->hParent>=sizeof(app_ui_pages)/sizeof(void*))
    {
        return;
    }
    lppage = (LPUIPAGE)app_ui_pages[lpscr->hParent];
    cur_val = ZGetCtrlValue(ctrl_id);
    if(lpscr->dExtStyle == (BYTE)ZSCR_HD)
    {
        px = lpscr->rect.left + lpscr->rect.width/2-12 + offsetx;
        py = lpscr->rect.top + lpscr->rect.height + 10 + offsety;
    }
    else
    {
        px = lpscr->rect.left + lpscr->rect.width/2-12 + offsetx;
        py = lpscr->rect.top + lpscr->rect.height + 10 + offsety;
    }
    if(cur_val<10)
        ZPrintf_XY(px, py, bcolor, fcolor,lppage->nWinno, " %d ", cur_val );
    else if(cur_val<100)
        ZPrintf_XY(px, py, bcolor, fcolor,lppage->nWinno, " %d", cur_val );
    else
        ZPrintf_XY(px, py, bcolor, fcolor,lppage->nWinno, "%d", cur_val );
}
void zUpdateScrollBar(WORD ctrl_id, int value, U8 mode)
{
    LPUISCROLLBAR lpscr;
    U8 new_pos,sta=0;
    int pos;
    if(ctrl_id>= sizeof(app_ui_ctrls)/sizeof(void*))
    {
        return;
    }
    lpscr = app_ui_ctrls[ctrl_id];
    if(lpscr == 0)
    {
        return;
    }
    if(mode == 0)
    {
        pos = value;
    }
    else
    {
        ZGetCtrlParam(ctrl_id, ZVAL_VALUE, &new_pos);
        sta = new_pos&0x80;
        pos = new_pos&0x7f;
        pos += value*lpscr->sScrollbarValue.step;
    }
    if(pos > lpscr->sScrollbarValue.Max)pos = lpscr->sScrollbarValue.Max;
    else if(pos < lpscr->sScrollbarValue.Min)pos = lpscr->sScrollbarValue.Min;
    ZSetCtrlParam(ctrl_id, ZVAL_VALUE, pos|sta);
    DrawScrollbarControl(lpscr);
}


#ifdef Keil_C51
int ScrCallBack ( msg_data_t * MSG )reentrant
#else
int ScrCallBack ( msg_data_t * MSG )
#endif
{
    LPUISCROLLBAR lpscr;
    UIRECT rect;
    U8 sta;

    HUICTRL hCtrl;
    U8 uMsg;
    WORD param1;
    WORD param2;
    WORD param3;
    hCtrl=MSG->mitem;
    uMsg=MSG->mtype;
    param1=MSG->mparam;
    param2=MSG->mparal;
    param3=MSG->mparan;
    lpscr = app_ui_ctrls[hCtrl];
    if(lpscr->type[0] != (U8)WGUI_SCROLLBAR)
    {
        return APP_ERROR;
    }
    switch(uMsg)
    {
        case MWUI_PUBLIC:
            switch(param1)
            {
                case MWUI_UPDATE:
                {
                    zUpdateScrollBar(hCtrl, param3, param2);
                    zprintCtrlCoreValue(hCtrl, 180, 350);
                    return 0;
                }
                break;
                case MWUI_CREATE:
                    //switch(hCtrl) // you can init different scrollbar here by hCtrl
                    if(!InvalidPageNumber(lpscr))
                    {
                        LPUIPAGE lpPage;
                        lpPage = app_ui_pages[lpscr->hParent];
                        //ZSetCtrlParam(hCtrl, ZVAL_VALUE, lpscr->sScrollbarValue.Default);
                        if(lpPage->active_ctrl_id == hCtrl)
                        {
                            ZSetCtrlParam(hCtrl, ZVAL_VALUE, lpscr->sScrollbarValue.Default);
                        }
                        else
                        {
                            ZSetCtrlParam(hCtrl, ZVAL_VALUE, lpscr->sScrollbarValue.Default|0x80);
                        }
                        InitCtrlValue(hCtrl);
                        DrawScrollbarControl(lpscr);
                        zprintCtrlCoreValue(hCtrl, 20, 380);
                    }
                break;
            }
        break;
        case MWUI_KEYPRESS:
            switch(param1)
            {
                case KEYUI_UP:
                case KEYUI_RT:
                    zUpdateScrollBar(hCtrl, 1, 1);
                break;
                case KEYUI_LEFT:
                case KEYUI_DN:
                    zUpdateScrollBar(hCtrl, -1, 1);
                break;
            }
        break;
        case MWUI_NOTIFY:
        break; 
        case MWUI_TOUCH:
            switch(param1)
            {
            case KWUI_TOUCH_UP:
                {
                    U8 new_pos;
                    LPBASICWIN lpbasic;
                    rect = lpscr->sScrollbarinfo.rcTrack;
                    ZGetCtrlParam(hCtrl, ZVAL_VALUE, &new_pos); //get scrollbar ->sScrollbarValue.curValue
                    sta = new_pos&0x80;
                    new_pos = new_pos&0x7f;
                    lpbasic = app_ui_ctrls[hCtrl];
                    ZInvalidateMouseFocus(lpbasic->hParent);
                    if(lpscr->dExtStyle == ZSCR_HD)
                    {
                        rect.left = lpscr->sScrollbarinfo.rcTrack.left + (new_pos-lpscr->sScrollbarValue.Default)*lpscr->sScrollbarValue.step;
                    }
                    else
                    {
                        rect.top = lpscr->sScrollbarinfo.rcTrackBack.top + (new_pos-lpscr->sScrollbarValue.Default)*lpscr->sScrollbarValue.step;
                    }
                    if(ZIsPointInScrRect(param2, param3, &lpscr->sScrollbarinfo.rcUp, hCtrl) == 0)
                    {
                        new_pos -= lpscr->sScrollbarValue.step;
                        if(new_pos < lpscr->sScrollbarValue.Min)new_pos = lpscr->sScrollbarValue.Min;
                        ZSetCtrlParam(hCtrl, ZVAL_VALUE, new_pos);
                        DrawScrollbarControl(lpscr);
                    }
                    else if(ZIsPointInScrRect(param2, param3, &lpscr->sScrollbarinfo.rcDown, hCtrl) == 0)
                    {
                        new_pos += lpscr->sScrollbarValue.step;
                        if(new_pos > lpscr->sScrollbarValue.Max)new_pos = lpscr->sScrollbarValue.Max;
                        ZSetCtrlParam(hCtrl, ZVAL_VALUE, new_pos);
                        DrawScrollbarControl(lpscr);
                    }
                    else if(ZIsPointInScrRect(param2, param3, &rect, hCtrl) == 0)
                    {
                    }
                    else if(ZIsPointInScrRect(param2, param3, &lpscr->sScrollbarinfo.rcTrackBack, hCtrl) == 0)
                    {
                        if(new_pos < lpscr->sScrollbarValue.Default)
                        {
                            new_pos += lpscr->sScrollbarValue.Scale*lpscr->sScrollbarValue.step;
                            if(new_pos > lpscr->sScrollbarValue.Max)new_pos = lpscr->sScrollbarValue.Max;
                        }
                        else
                        {
                            new_pos -= lpscr->sScrollbarValue.Scale*lpscr->sScrollbarValue.step;
                            if(new_pos < lpscr->sScrollbarValue.Min || new_pos>lpscr->sScrollbarValue.Max)
                                new_pos = lpscr->sScrollbarValue.Min;
                        }
                        ZSetCtrlParam(hCtrl, ZVAL_VALUE, new_pos|sta);
                        DrawScrollbarControl(lpscr);
                    }
                }
            break;
            case KWUI_TOUCH_DOWN:
            {
                ZSetCtrlFocus(hCtrl, 1);
            }
            break;
            }
        break; 
    }
    return 0;
}


#ifdef Keil_C51
int AniCallBack ( msg_data_t * MSG )reentrant
#else
int AniCallBack ( msg_data_t * MSG )
#endif
{
    U8 npos;
    U16 cpos;
    static U16 counter=0; 
    LPUIANIMATOR lpani;    
    HUICTRL hCtrl;
    U8 uMsg;
    WORD param1;
    hCtrl=MSG->mitem;
    uMsg=MSG->mtype;
    param1=MSG->mparam;
    lpani = app_ui_ctrls[hCtrl];
    if(lpani->type[0] != (U8)WGUI_ANIMATER)
    {
        return APP_ERROR;
    }
    switch(uMsg)
    {
        case MWUI_PUBLIC:
            switch(param1)
            {
                case MWUI_UPDATE:
                break;
                case MWUI_TIMER:
                    counter ++;
                    ZGetCtrlParam(hCtrl, ZVAL_VALUE, &npos);
                    cpos = npos;
                    if(cpos > lpani->sAnimateInfo.endImageID) cpos = lpani->sAnimateInfo.startImgaeID;
                    if(cpos < lpani->sAnimateInfo.startImgaeID) cpos = lpani->sAnimateInfo.startImgaeID;
                    TWLL_Icon_Create(cpos, IconShowOnCanvas);
                    cpos ++;
                    ZSetCtrlParam(hCtrl, ZVAL_VALUE, cpos);
                break;
                case MWUI_DESTROY:
                    {
                        U8 timerid;
                        timerid = npos;
                        //if(timerid != 255)
                        {
                            ZKillTimer(hCtrl);
                        }
                    }
                break;
                case MWUI_INIT:
                break;
                case MWUI_CREATE:
                    {
                        U8 timerid;
                        TWLL_Icon_Create(lpani->sAnimateInfo.defaultImageID, IconShowOnCanvas);
                        timerid = ZCreateTimer(hCtrl, lpani->sAnimateInfo.timerInterval, lpani->sAnimateInfo.timerStart, lpani->sAnimateInfo.OnOff);
                        ZSetCtrlParam(hCtrl, ZVAL_VALUE, lpani->sAnimateInfo.defaultImageID+1);
                        InitCtrlValue(hCtrl);
                        //ZSetCtrlParam(hCtrl, ZVAL_VALUE, timerid);
                    }
                break;
            }
        break;
        case MWUI_KEYPRESS:
            switch(param1)
            {
                case KEYUI_TOUCH:
                break;
            }
        break;
        case MWUI_NOTIFY:
        break; 
    }
    return 0;
}


static void zprintCtrlCoreValue(int id, int x, int y)
{
    U8 val = 0;
    ZGetCtrlParam(id, ZVAL_VALUE, &val);


}
void ZApp_Clear_Canvas_Page( U8 user_canvas_page_id, U16 color)
{
    UIControlRect_struct  rect_show[1];
    RectangleControl_struct rect_cntr;
    CANVAS * pCanvas;
    pCanvas = &User_Canvas[user_canvas_page_id];
    rect_cntr.XY1.X = 0;
    rect_cntr.XY1.Y = 0;
    rect_cntr.XY2.X = pCanvas->width;
    rect_cntr.XY2.Y = pCanvas->height;
    rect_cntr.Color = color;
    rect_cntr.BorderColor = 0;
    rect_cntr.BorderWidth = 0;
    rect_show[0].Canvas_ID=user_canvas_page_id;
    rect_show[0].DispControl=&rect_cntr;
    TWLL_Rect_Create(0,rect_show);
}


void ZApp_Set_Pallette(U8 canvas_id, U8 pal_id)
{
    TWLL_Canvas_Pallette_Set(canvas_id, pal_id);
}




U8 zGetPageStartPoint(ZPoint *pt, LPUIPAGE lpPage)
{
    if(lpPage)
    {
        pt->px = (U16)(User_Canvas[lpPage->nWinno].Screen_X);
        pt->py = (U16)(User_Canvas[lpPage->nWinno].Screen_Y);
        return 0;
    }
    else
    {
        return 255;
    }
}


void InitCoreTimer(DWORD loop_counter)
{
    zinit_core_timer(loop_counter);
}


void ProcessMessage(U8 msg_type, U16 msg_item, U16 param, U16 paral, U16 paran)
{
    ZProcessMessage(msg_type, msg_item, param, paral, paran);
}


void SendMesssage(U8 msg_type, U16 msg_item, U16 param, U16 paral, U16 paran)
{
    ZSendMesssage(msg_type, msg_item, param, paral, paran);
}


U16 GetMessage(msg_data_t * msg)
{
    return ZGetMessage(msg);
}


void ZAPPTimerRun()
{
    ZUITimerRun();
}


